<template>
  <div class="car-team">
    <!-- 搜索栏 -->
    <div class="search-block">
      <div class="item-msg">
        <label>状态</label>
        <el-select v-model="searchForm.status" placeholder="全部" size="small">
          <el-option label="全部" value></el-option>
          <el-option label="待审核" value="0"></el-option>
          <el-option label="已通过" value="1"></el-option>
          <el-option label="已拒绝" value="-1"></el-option>
        </el-select>
      </div>
      <div class="item-msg">
        <label>精确查找</label>
        <el-input
          placeholder="UID/手机/邮箱"
          v-model="searchForm.search"
          class="input-with-select"
          size="small"
          clearable
        ></el-input>
      </div>
      <div class="item-msg">
        <label>提交日期</label>
        <el-date-picker
          size="small"
          v-model="searchForm.times"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </div>
      <el-button class="search-btn" @click="search()" size="small" type="primary">查询</el-button>
      <el-button
        class="search-btn"
        size="small"
        type="primary"
        @click="visible.showDialog = true"
      >新增车队长</el-button>
      <div class="whole-net">
        <label>每日司机奖励总量：</label>
        <span style="margin-right:5px">{{reward}}</span>
        <el-popover placement="right" trigger="click" v-model="showPopover">
          <el-input
            size="mini"
            width="100"
            style="width:120px;margin-right:5px"
            placeholder="每日投放总量"
            v-model="setReward"
          ></el-input>
          <el-button size="mini" type="primary" @click="_editDayReward(setReward)">确定</el-button>
          <i class="iconfont iconweb-icon-" slot="reference"></i>
        </el-popover>
      </div>
    </div>
    <!-- 数据表 -->
    <div class="data-table">
      <el-table
        size="medium"
        v-loading="loading"
        element-loading-text="拼命加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(211, 215, 212, 0.5)"
        :data="tableData"
        stripe
        header-cell-class-name="headerStyle"
        style="width: 100%"
      >
        <el-table-column align="center" prop="id" label="ID" width="120"></el-table-column>
        <el-table-column align="center" prop="captain_name" label="姓名" width="120"></el-table-column>
        <el-table-column align="center" prop="mobile" label="手机"></el-table-column>
        <el-table-column align="center" prop="email" label="邮箱"></el-table-column>
        <el-table-column align="center" prop="contact" label="微信" width="170"></el-table-column>
        <el-table-column align="center" prop="name" label="车队名" width="180"></el-table-column>
        <el-table-column align="center" prop="created_at" label="提交时间"></el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="item">{{item.row.status|statusFilter}}</template>
        </el-table-column>
        <el-table-column align="center" prop="inviter" label="操作">
          <template slot-scope="item" v-if="item.row.status===0">
            <el-button type="text" @click="audit(item.row.id,1)">通过</el-button>
            <el-button type="text" @click="audit(item.row.id,2)">拒绝</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pages
        :pages="searchForm"
        :total="total"
        @pageSizeChange="pageSizeChange"
        @pageChange="pageChange"
      ></pages>
      <leader-dialog :visible="visible" @success="search()"></leader-dialog>
    </div>
  </div>
</template>

<script>
import Pages from "@/components/PageBox.vue";
import LeaderDialog from "./AddLeader";
import {
  getAuditList,
  audit,
  getDayReward,
  editDayReward
} from "./carTeam.service";

export default {
  components: {
    Pages,
    LeaderDialog
  },
  filters: {
    statusFilter(val) {
      switch (val) {
        case 0:
          return "待审核";
          break;
        case 1:
          return "已通过";
          break;
        case -1:
          return "已拒绝";
          break;
        default:
          break;
      }
    }
  },
  data() {
    return {
      loading: false,
      total: 0,
      tableData: [],
      visible: {
        showDialog: false
      },
      searchForm: {
        page: 1,
        size: 10
      },
      reward: "",
      setReward: "",
      showPopover: false
    };
  },
  watch: {
    "searchForm.times": function(time) {
      if (time) {
        this.searchForm.start_date = time[0];
        this.searchForm.end_date = time[1];
      } else {
        this.searchForm.start_date = this.searchForm.end_date = "";
      }
    }
  },
  mounted() {
    this.search();
    this._getDayReward();
  },
  methods: {
    async search() {
      this.loading = true;
      const res = await getAuditList(this.searchForm);
      this.tableData = res.data.data || [];
      this.total = res.data.total;
      this.loading = false;
    },
    audit(fleet_id, operation) {
      let param = {
        fleet_id,
        operation
      };
      let info = "";
      switch (operation) {
        case 1:
          info = "通过";
          break;
        case 2:
          info = "拒绝";
          break;
        default:
          break;
      }
      this.$confirm(`确定${info}此用户的请求？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          audit(param).then(res => {
            if (res.code === 1) {
              this.$notify.success({
                title: "提示",
                message: "操作成功"
              });
              this.search();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    //获取每日投放量
    async _getDayReward() {
      const res = await getDayReward();
      this.reward = res.data.reward_base_num || "";
    },
    //修改每日投放量
    async _editDayReward(setReward) {
      if (setReward == "") {
        this.$notify({
          title: "提示",
          message: "每日投放量不能为空",
          type: "warning",
          duration: 1500
        });
        return;
      }
      const res = await editDayReward(setReward);
      if (res.code === 1) {
        this.$notify({
          title: "提示",
          message: "修改成功",
          type: "success",
          duration: 1500
        });
        this.showPopover = false;
        this._getDayReward();
      }
    },
    pageChange(val) {
      this.searchForm.page = val;
      this.search(this.searchForm);
    },
    pageSizeChange(val) {
      this.searchForm.size = val;
      this.search(this.searchForm);
    }
  }
};
</script>

<style lang="scss">
.car-team {
  // position: relative;
  min-height: calc(100vh - 100px);

  .search-block {
    position: relative;
    width: 100%;
    padding: 25px 30px;
    border-radius: 5px;
    background: #fff;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;

    .item-msg {
      display: inline-block;
      margin-right: 20px;
      height: 60px;
      line-height: 60px;
      font-size: 1 size= "medium" 4;
      color: $admin_font_param;

      label {
        display: inline-block;
        max-width: 90px;
        text-align: right;
        margin-right: 10px;
      }

      .el-input {
        width: 150px;
      }

      .el-select {
        width: 150px;
      }

      .el-date-editor {
        width: 270px;
      }
    }

    .search-btn {
      margin-left: 25px;
    }
  }

  .data-table {
    // position: relative;
    padding-bottom: 20px;
    background: #fff;
    border-radius: 5px;
    margin-top: 30px;
    box-shadow: rgba(0, 0, 0, 0.1) 0 0 15px;
  }
  .whole-net {
    display: inline-block;
    position: absolute;
    top: 10px;
    right: 15px;
    color: $admin_font_param;
    .iconweb-icon- {
      color: #409eff;
    }
  }
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }

  .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */
 {
    opacity: 0;
  }
}
</style>
